package com.test.controllers;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.test.services.EmailSenderService;
import com.test.services.RegisterUserService;

public class Registration1Activity extends Activity {

	private static final String TAG = "RegistrationPart1Activity";
	private Button nextButton;

	EmailSenderService email = new EmailSenderService();

	String emailaddress = "";
	String password = "";
	RegisterUserService registerUserService = new RegisterUserService();
	String username = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.registration_part1);

		final EditText txtUserName = (EditText) findViewById(R.id.editText1);
		final EditText txtPassword = (EditText) findViewById(R.id.editText2);
		final EditText txtEmail = (EditText) findViewById(R.id.editText3);



		nextButton = (Button) findViewById(R.id.button1);
		nextButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				username = txtUserName.getText().toString();

				password = txtPassword.getText().toString();

				emailaddress = txtEmail.getText().toString();

				if (username.matches("") || password.matches("")
						|| emailaddress.matches("")) {
					Log.d(TAG,
							"One or more editboxes empty. Throwing dialogbox");
					dialogBox("Please fill in all the details on this screen.",
							"Missing details.");
				} else {

					Log.d(TAG, "Preparing intent to RegistrationPart2Activity");
					Intent registrationPart2Intent = new Intent(
							Registration1Activity.this,
							RegistrationPart2Activity.class);

					registrationPart2Intent.putExtra("username", username);
					Log.d(TAG, "Intent Username: " + username);
					registrationPart2Intent.putExtra("password", password);
					Log.d(TAG, "Intent password: " + password);
					registrationPart2Intent.putExtra("email", emailaddress);
					Log.d(TAG, "Intent email: " + emailaddress);

					Log.d(TAG,
							"Navigating via intent to RegistrationPart2Activity");
					startActivity(registrationPart2Intent);

				}

			}

		});
	}

	private void dialogBox(String messageDisplayText, String Title) {

		String msg = messageDisplayText;
		final String title = Title;

		AlertDialog.Builder builder = new AlertDialog.Builder(
				Registration1Activity.this);

		builder.setMessage(msg).setCancelable(false)
				.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int id) {
						Log.d(TAG, "Ok Pressed on DialogBox " + title);
					}
				});
		AlertDialog alert = builder.create();
		alert.setTitle(title);
		alert.show();
	}

}
